<?php
/**
 * 重签脚本 计划任务执行
 *
 */
date_default_timezone_set('Asia/Shanghai');

define('DS', DIRECTORY_SEPARATOR);
define('ROOT_PATH', realpath(dirname(__FILE__).DS).DS);
define('PACK', realpath(dirname(__FILE__).DS.'..'.DS).DS);
define('TRUE_PAK', PACK.'resources'.DS.'truepack'.DS);
define('PUSH_PAK', PACK.'resources'.DS.'pushdir'.DS);
define('ORIGIN_PAK', PACK.'resources'.DS.'originpak'.DS);
define('PERFILE', PACK. 'resources'.DS.'perfile'.DS);
ignore_user_abort(true);
set_time_limit(0);
//mysql database address
define('DB_HOST','127.0.0.1');
//mysql database user
define('DB_USER','root');
//database password
define('DB_PASSWD','123456');
//database name
define('DB_NAME','package');

require_once(ROOT_PATH.'mysqli.php');
require_once(ROOT_PATH.'macpackgesign.php');

strongsign();

/**
 * 重签计划任务
 * @author Giles <giles.wang@icloud.com>
 * @return void
 */
function strongsign()
{
    $db = MySqlii::getInstance();
    
    $check = $db->once_fetch_array('select count(*) as num from g5_strongsign_cron_job where status = 2');
    if($check['num'] != 0) {
        saveLog('有强签打包任务跳过');
        return ;
    }

    $file = "ok.txt";
    $result = $db->query('select * from g5_strongsign_cron_job  where status = 1 order by id  desc');
    $array=$db->fetch_all($result);

    if(count($array)>20){
        checkOK($file);
    }    

    foreach($array as $value){
         if(empty($value['isbackpkg'])){
            $perarray=explode(",",$value['perfileId']);
        }else{
            $perarray=explode(",",$value['backperfileId']);
        }
        foreach($perarray as $key=>$val){

            $perfileInfo = $db->once_fetch_array("select * from g5_perfile where id =". $val);
            $db->query("update g5_strongsign_cron_job  set status = 2 where id = ".$value['id']);

            if($value['packType'] == 5) {
                $newPakName = strresin($value,$perfileInfo,0);
            } else {
                saveLog('开始打包：' . $value['packName']);
                if($key==0&&empty($value['isbackpkg'])) {
                   $newPakName = strresin($value,$perfileInfo,0);
                }else{
                    $newPakName = strresin($value,$perfileInfo,1);
                }
            }
            if($newPakName['error'] != '0000') {
                $db->query("update g5_strongsign_cron_job set status = 4, errorinfo = '{$newPakName['error']}' where id = ".$value['id']);
            } else {
                $up = array(
                    'packSize' => format_bytes(filesize(TRUE_PAK."strpsign/".$newPakName['message'])),
                    'packName' => $newPakName['message'],
                    'trueTime' => time(),
                    'status'   => 3
                );
                if($key==0) {
                     echo "/n";
                     echo "1111";
                     echo "一个包 生成备份包";
                    saveLog('打包成功1：' . $value['packName']);
                    saveLog('备份包111111：' . $up['packName']);
                    if (empty($value['isbackpkg'])) {
                       echo "/n";
                        echo "zhiyige";
                        $db->query("update g5_strongsign_cron_job set status = {$up['status']},specail= '0', packSize = '{$up['packSize']}',newPackName = '{$up['packName']}', addTime = {$up['trueTime']} where id = " . $value['id']);
                    } else {
                        echo "/n";
                         echo "dandu";
                        $db->query("update g5_strongsign_cron_job set status = {$up['status']},specail= '0',backPackName = '{$up['packName']}', addTime = {$up['trueTime']} where id = " . $value['id']);
                    }
                }else{
                    echo "/n";
                    echo "22222";
                    saveLog('有备份包223232：' . $up['packName']);
                        $db->query("update g5_strongsign_cron_job set status ={$up['status']},specail= '0', backPackName = '{$up['packName']}' where id = " . $value['id']);
                    
                }
            }

        }
        if(file_exists(ROOT_PATH.$file)) {
               exec("rm -rf ". ROOT_PATH.$file);
         } 
    }

}

function checkOK($file)
{
    if(file_exists(ROOT_PATH.$file)) {
        echo "\n等待..";
        sleep(10);
        checkOK($file);
    } else {
        exec('mkdir '.ROOT_PATH.$file);
        return true;
    }
}


function  saveLog($message)
{
    $time = date('Y-m-d H:i:s');

    file_put_contents(ROOT_PATH.'qiangqian.log', $time. '--'. $message. "\n", FILE_APPEND);
}

//获取文件大小
function format_bytes($size) {
    $units = array(' B', ' KB', ' MB', ' GB', ' TB');
    for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
    return round($size, 2).$units[$i];
}












